<?php
/**
 * Delicioso
 *
 * Using del.icio.us (and shortText) as a blogging platform.
 *
 * @package Web
 * @author Bruno Pedro <bpedro@computer.org>
 **/
class Delicioso
{

	/**
	 * del.icio.us username
	 *
	 * @var string
	 **/
	private $username;
	
	/**
	 * del.icio.us password
	 *
	 * @var string
	 **/
	private $password;
	
	/**
	 * The blog URL.
	 *
	 * @var string
	 **/
	private $url;
	
	/**
	 * The del.icio.us object
	 *
	 * @var mixed
	 **/
	private $delicious;

	/**
	 * Constructor
	 *
	 * @param $username string del.icio.us username.
	 * @param $password string del.icio.us password.
	 * @param $url string The blog URL (optional).
	 * @return void
	 **/
	function __construct ($username, $password, $url =  null)
	{
		//
		// Save a local copy of the username and password.
		//
		$this->username = $username;
		$this->password = $password;

		//
		// Save a local copy of the URL to be used.
		//
		if (empty($url)) {
			$this->url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		} else {
			$this->url = $url;
		}
		
		if (!empty($this->url) &&
			!empty($this->username) &&
			!empty($this->password)) {

				//
				// Start a del.icio.us service object.
				//
				require_once 'Delicious.php';
				$this->delicious =& new Services_Delicious($username, $password);
				
				//
				// Set the del.icio.us API URL.
				//
				$this->delicious->_apiUrl = 'https://api.del.icio.us/v1/';
		}
	}
	
	/**
	 * Save a blog post.
	 *
	 * @param $title string The post title.
	 * @param $body string The post body.
	 * @param $tags array The post tags.
	 * @param $id string Post ID (if not present it will be generated).
	 * @return mixed The result of the operation.
	 **/
	public function savePost ($title, $body, $tags = array(), $id = null, $date = null)
	{
		if ($this->delicious) {
			//
			// Generate an id for this post.
			//
			if (empty($id)) {
				$id = md5($title . $body);
			}
			
			//
			// Convert line breaks to <br />'s.
			//
			$body = nl2br($body);

            //
            // Post to shortText
            //
            require_once 'ShortText.php';
            $short =& new ShortText;
            $shortTextId = $short->post($body);
            $body = null;
			
			//
			// Set internal tags.
			//
			$metaTags = array('blog.url:' . $this->url,
						      'blog.post.status:publish',
						      'blog.post.id:' . $id);
            if ($shortTextId) {
                array_push($metaTags, 'shortText:' . $shortTextId);
            }

			//
			// Post to del.icio.us.
			//
			return $this->delicious->addPost($this->url . '?id=' . $id,
											 $title,
											 $body,
											 implode(' ', array_merge($metaTags, $tags)),
											 $date);
		}
		return false;
	}
	
	/**
	 * Get all posts for this URL.
	 *
	 * @return mixed The result of the operation.
	 **/
	public function getPosts ()
	{
		if ($this->delicious) {
			//
			// Set internal tags.
			//
			$tags = array('blog.url:' . $this->url,
						  'blog.post.status:publish');

			//
			// Get posts.
			//
			$posts = $this->delicious->getRecentPosts($tags);
            return $this->getBodiesFromShortText($posts);
		}
		return false;
	}
	
	/**
	 * Get a single post identified by its id.
	 *
	 * @param $id integer The post id.
	 * @return mixed The result of the operation.
	 **/
	public function getPost ($id)
	{
		if ($this->delicious) {
			//
			// Set internal tags.
			//
			$tags = array('blog.post.id:' . $id,
						  'blog.post.status:publish');

			//
			// Get post.
			//
			$posts = $this->delicious->getRecentPosts($tags);
			return $this->getBodiesFromShortText($posts);
		}
		return false;
	}

	/**
	 * Edit a post.
	 *
	 * @param string $id The post id.
	 * @param string $title (optional) The post title.
	 * @param string $body (optional) The post body.
	 * @param array $tags (optional) The post tags.
	 * @return void
	 **/
	public function editPost ($id, $title = null, $body = null, $tags = array())
	{
		$post = $this->getPost($id);
		if ($post) {
		    $timeStamp = strtotime($post[0][$time]);
			return $this->savePost($title, $body, $tags, $id, $post[0]['time']);
		}
		return false;
	}
	
	/**
	 * Gets bodies from the shortText service.
	 *
	 * @param $posts array The posts.
	 * @return array The posts array.
	 **/
	private function getBodiesFromShortText ($posts = array())
	{
		//
		// Start a ShortText object.
		//
        require_once 'ShortText.php';
        $short =& new ShortText;

		//
		// Get bodies from shortText for all posts with
		// an empty body that have the 'shortText:*' tag.
		//
        foreach ($posts as $idx => $post) {
            if (empty($post['extended'])) {
                foreach ($post['tag'] as $tag) {
                    list($tagName, $tagValue) = explode(':', $tag);
                    if ($tagName == 'shortText') {
                        $shortTextId = $tagValue;
                        break;
                    }
                }
                $posts[$idx]['extended'] = $short->get($shortTextId);
            }
        }
		return $posts;
	}
}
?>
